<template>
  <cu-editor ref="editorRef" :pathPrefix="pathPrefix" :url="uploadUrl" :name="uploadName"
             :header="header"
             :content="content" @save="onSave" @myInput="onInput"></cu-editor>
</template>

<script setup lang="ts">

import CuEditor from "@/components/common/cu-editor/cu-editor.vue";
import {computed, ref, watch} from "vue";
import {onLoad, onUnload} from "@dcloudio/uni-app";

import {imagePathPrefix, uploadFile} from '/settings'
import {debounce} from "@/utils";
import MrCustom from "@/components/common/MrCustom.vue";

const editorRef=ref()
const uploadUrl = uploadFile.url
const uploadName = uploadFile.name
const pathPrefix = imagePathPrefix
const header = ref()
const content = ref('')
const eValue = ref()



onLoad((option) => {
  if (option.content) {
    let tmp = JSON.parse(decodeURIComponent(option.content))
    content.value = tmp ? tmp : ''
  }
  uni.disableAlertBeforeUnload()
})
const onInput=debounce(()=>{
  console.log('输入')
  uni.enableAlertBeforeUnload({
    message: "当前编辑未保存，是否退出"
  })
},500)

const onSave = (e: any) => {
  eValue.value = e
  uni.disableAlertBeforeUnload()
}

onUnload(() => {
  if (eValue.value) {
    let e = eValue.value
    let html = e.html
    let containsImage = html.search(/<img /i) >= 0 //内容是否包含图片标签
    let txt = e.text.replace(/(^\s*)|[\r\n]|(\s*$)/g, "") //去掉换行符和两端空格
    uni.$emit('save', {
      html: txt == "" && !containsImage ? txt : html
    })
  }
})

</script>

<style scoped>

</style>